package com.baosight.sczx.zm.ck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.sczx.zm.cg.domain.ZMCGRecord;
import com.baosight.sczx.zm.ch.domain.ZMCHMineReport;
import com.baosight.sczx.zm.ck.domain.ZMCKTCCR;
import com.baosight.sczx.zm.ck.domain.ZMCKTCCRReport;
import com.baosight.sczx.zm.ck.domain.ZMCKYXFAReport;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;

/**
 * 演习情况反馈
 * <p>Title: ServiceZMCK11.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCK11 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCK01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
         String fdYxId=null;
		
		if(inInfo.getString("fdYxId")!=null)
		fdYxId = inInfo.getString("fdYxId");
		StaticTools.setQueryParameters(inInfo, "fdYxId", fdYxId, true);//添加查询条件
		EiInfo  outInfo = super.query(inInfo, "ZMCKYXFAReport.query", new ZMCKYXFAReport());
		      
		
		outInfo.addBlock(StaticBlocks.getStauts5());
		outInfo.addBlock(getMines());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCKTCCR.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		 String fdYxId=null;
			
		if(inInfo.getString("fdYxId")!=null)
		fdYxId = inInfo.getString("fdYxId");
		
		EiInfo out=new EiInfo(); 
		ZMCKYXFAReport _dom = new ZMCKYXFAReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_TCCR_REPORT"));
			_dom.setFdYxId(fdYxId);
			_dom.setFdDepartmentId((String)UserSession.getInSessionProperty("orgCode"));
			_dom.setFdDepartment((String)UserSession.getInSessionProperty("orgName"));
		    this.dao.insert("ZMCKYXFAReport.insert", _dom);
     
	    }
	 	out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
 }

	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo outInfo = new EiInfo();
		ZMCKYXFAReport _dom = new ZMCKYXFAReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdYxId=null;
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			fdYxId = _dom.getFdYxId();
			this.dao.delete("ZMCKYXFAReport.delete", _dom);		
		}
		inInfo.set("fdYxId", fdYxId);
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo; 	
		
		
		
		
		
		
		
		
		
		
//		
//
//		EiInfo outInfo = null;
//		
//		List<String> _list = new ArrayList<String>();
//		Map<String,List<String>> params = new HashMap<String,List<String>>();
//
//		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
//		{
//
//			EiInfo _temp = super.delete(inInfo, "ZMCKYXFAReport.delete");
//	        outInfo = this.query(inInfo);
//	        outInfo.setMsg(_temp.getMsg());
//	        outInfo.setDetailMsg(_temp.getDetailMsg());
//		}
//		
//		return outInfo;
	}
	
}